package cc.chinagps.gateway.client.packet;

import cc.chinagps.gateway.client.bean.DeviceInfo;
import cc.chinagps.gateway.client.bean.TerminalType;
import cc.chinagps.gateway.client.packet.down.Packet01;
import cc.chinagps.gateway.client.packet.down.Packet02;
import cc.chinagps.gateway.util.HexUtil;
import cc.chinagps.gateway.util.Sm2Util;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;
import lombok.EqualsAndHashCode;


@EqualsAndHashCode(callSuper = true)
@Data
public class BasePacket extends AbstractPacket {
    public BasePacket() {
    }

    public BasePacket(DeviceInfo deviceInfo) {
        this.deviceInfo = deviceInfo;
    }

    public BasePacket(byte msgId, DeviceInfo deviceInfo) {
        this(msgId, (byte) 0xFF, deviceInfo);
    }

    public BasePacket(byte msgId, byte infoType, DeviceInfo deviceInfo) {
        Integer terminalType = deviceInfo.getTerminalType();
        this.msgId = msgId;
        this.msgIdHex = String.format("0x%02X", msgId & 0xFF);
        this.infoType = infoType;
        this.infoTypeHex = String.format("0x%02X", infoType & 0xFF);
        if (terminalType == TerminalType.OBD.getType()) {
            this.vin = deviceInfo.getVin();
        } else {
            this.vin = deviceInfo.getEnvNo();
        }
        this.version = deviceInfo.getVersion();
        this.encryptType = deviceInfo.getEncryptType();
        this.terminalNo = deviceInfo.getTerminalNo();
        this.deviceInfo = deviceInfo;
    }

    @Override
    public void decodeData(byte[] data) throws Exception {
    }

    @Override
    public byte[] encryptData(byte[] data, byte encryptType) throws Exception {
        switch (encryptType) {
            case 0x01:
            case 0x02:
                break;
            case 0x03:
                return Sm2Util.encrypt(deviceInfo.getPublicKey(), data);
            default:
                throw new Exception("encryptData error,unknow encrypt type");
        }
        return data;
    }

    @Override
    public byte[] decryptData(byte[] data, byte encryptType) throws Exception {
        switch (encryptType) {
            case 0x01:
            case 0x02:
                break;
            case 0x03:
                return Sm2Util.decrypt(deviceInfo.getPrivateKey(), data);
            default:
                throw new Exception("decryptData error,unknow encrypt type");
        }
        return data;
    }

    @Override
    public String toString() {
        return "BasePacket{" +
                "msgId=" + msgIdHex +
                ", infoType=" + infoTypeHex +
                ", vin='" + vin + '\'' +
                ", version=" + version +
                ", terminalNo='" + terminalNo + '\'' +
                ", encryptType=" + encryptType +
                ", obdSignature=" + obdSignature +
                ", signResult=" + signResult +
                ", xor=" + xor +
                ", source='" + source + '\'' +
                '}';
    }
    public static void main(String[] args) throws Exception {
        String s = "2301ff4c565335353233363938373431353437362b53473030383938383235383431343735383739350300860414d05c47bf43924b3b48dfee8319fa2867384fc643401c0cf07568e0d7c5d96fe591790b433758e692c2df827eed3227c5a5c4a1ea47b48ee3e42cbdf839a8ad19234165966d131db5e077ee15a2e4e2592063993564310d541bdfa1d9e39218522432481cdd09f6e3b4bdc2a5edb822748de16b9b808327ffafd98d2f44b5bb2adf293ad8208b51314c1397781d25b375b34333fa64134a043b712aecd0f77edaa31526727d2045cfabe1a1f028181f8c736bc3a8531481d59cddce874b57446990435b3e1481ba24";
        s = "230153473230323530383134313531333030312b01008504ac8cc69c859f3e14626e5f53a8601c844b23e705dfd37fe744dafc5f51145a7dd4c11b9fc4518d5fc51788c18c2f77f59116b3f705ce870dc6a6d29148097df0db07b51a63a2da8e209a21c3ad87a0d549857120fec0045bc3c5591e77b8cb1751d5e633941571bf6c13ae64dee98b9b20f55f40657783bb0fcaa7870d21312909cd3dd60624";
        s = "230153473230323530383134313531333030312b030085049a77d98b2d8dd443e1e0f5f70c543eafa9676e38b9565e5fc05adf6afeda2e4050118eeb71ad37a35e1ba8667c6afd7540470a4eabf9c0e6bb1a3e377dae29df8e71d2ef04059fe208bdfa8ce965937c3dc319842537582dd26b035c357ecf5428e98cac36d32cbe7ab61382fb8fdb51aa69e64504a4a69dcc11c67413ed9947d91c1780ee24";
        ByteBuf in = Unpooled.copiedBuffer(HexUtil.hexToBytes(s));
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setPublicKey("048e4e04119434515a696010817d0b9b5d0ca41157a60bec3f47b8fb16a1a69f34080b478a074b4b696c0497ad2c47d0d0ac65fdf273777703d568d0ed6f7b515d");
        deviceInfo.setPrivateKey("8cb30a2a9faedea033aaf7aa78896321a3c6fd9a6f5f876ec6f495c52285da5e");
        deviceInfo.setTerminalType(TerminalType.ELECTRONIC_LABEL.getType());
        BasePacket packet = new BasePacket(deviceInfo);
        packet.decode(in);
        System.out.println(packet);
        Packet01 packet01 = new Packet01(packet);
        packet01.decode();
        System.out.println(packet01);
    }
}
